package com.wiscamp.ninechapters.problems.infrastructure.mappers.exams;

import com.wiscamp.ninechapters.problems.infrastructure.po.exams.ExamView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;

/**
 * 考试数据访问对象
 *
 * @author yifeng
 */
@Repository
public interface ExamViewMapper extends JpaRepository<ExamView, Long>, JpaSpecificationExecutor<ExamView> {
    /**
     * 通过代码获取考试
     *
     * @param examCode
     * @param organizationId
     * @return
     */
    @Query(value = "select e from ExamView e where e.organizationId = :organizationId and e.examCode = :examCode")
    ExamView findByExamCode(String examCode, long organizationId);

    /**
     * 通过名称获取考试列表
     *
     * @param examName
     * @param organizationId
     * @return
     */
    @Query(value = "select e from ExamView e where e.organizationId = :organizationId and e.examName = :examName")
    Collection<ExamView> findListByExamName(String examName, long organizationId);

    /**
     * 通过机构编号获取考试列表
     *
     * @param organizationId
     * @return
     */
    @Query(value = "select e from ExamView e where e.organizationId in (:organizationId)")
    Collection<ExamView> findListByOrganizationId(long organizationId);

    /**
     * 分页获取考试列表
     *
     * @param spec
     * @param pageable
     * @return
     */
    Page<ExamView> findAll(Specification<ExamView> spec, Pageable pageable);
}